/**
 * Course Project for User Interface Programming I, 2011
 * Group 19
 */
package justdoit.view;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import justdoit.actions.ChangeUserAction;
import justdoit.controller.LanguageManager;
import justdoit.controller.TaskManager;
import justdoit.model.User;

/**
 * The status bar to display current user name, current time, and so on
 * @author Sadan, modified by Can.
 */
public class StatusBar extends JPanel {

    private JLabel labelUser = new JLabel();
    private JLabel labelDate = new JLabel();
    private User currentUser;

    public StatusBar(User user) {
        this.currentUser = user;
        initComponents();
    }

    // edited by Can.
    private void initComponents() {
        labelUser.setText(LanguageManager.getString("label.showUserName") + " " + currentUser.getUserName());
        labelDate.setText(/*LanguageManager.getString("label.showDate") + " " + */updateTime() + " "); //here we need also an update time function to display the current time
        now();  // start the timer
        labelUser.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // double clicks by left button
                if (e.getComponent().isEnabled() && e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                    // not a good way, but it works. :)
                    ChangeUserAction switchUser = new ChangeUserAction();
                    switchUser.actionPerformed(null);
                }
            }
        });
        this.setLayout(new BorderLayout());
        this.add(BorderLayout.WEST, labelUser);
        this.add(BorderLayout.EAST, labelDate);

    }

    // edited by Can
    public String now() {
        new Timer(1000, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                // here no need to say "Date" or something like that.. // Can
                labelDate.setText(/*LanguageManager.getString("label.showDate") + " " + */updateTime() + " ");
            }
        }).start();

        Calendar cal = Calendar.getInstance();
        String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; /*"yyyy-MM-dd HH:mm:ss"*/
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

        return sdf.format(cal.getTime());
    }

    // edited by Can, set the time format according to the user's locale
    public String updateTime() {
        return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, this.currentUser.getLocale()).format(new Date());
        //Calendar cal = Calendar.getInstance();
        //String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; /*"yyyy-MM-dd HH:mm:ss"*/
        //SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

//        Calendar a = td.getReminderDate2();
//        long diff = a.getTimeInMillis();
//        if (diff == 0) {
//            System.out.print("reminder has come!!");
//        }

//        if (td.getReminderDate().compareTo(sdf.format(cal.getTime())) == 0) {
//            System.out.print("reminder has come!!");
//            //JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.");
//        }
        //return sdf.format(cal.getTime());
    }

    public void updateUserNameLabel() {
        labelUser.setText(LanguageManager.getString("label.showUserName") + " " + TaskManager.getCurrentUser().getUserName());
    }
}
